package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.facade.cda.dto.ReqDto;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.mzzy.*;
import com.jy.rhin.infra.cda.service.MenZhenZhaiYaoService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.MenZhenZY;
import com.jy.rhin.model.ValueUnit;
import com.jy.rhin.model.body.feiyong.FeiYong;
import com.jy.rhin.model.body.shiyanshijiancha.ShiYanShiJCJY;
import com.jy.rhin.model.body.shoushucaozuo.ShouShu;
import com.jy.rhin.model.body.yongyao.YongYao;
import com.jy.rhin.model.body.zhenduanjilu.MenZhenZD;
import com.jy.rhin.model.body.zhengzhuang.ZhengZhuang;
import com.jy.rhin.model.body.zhiliaojihua.ZhiLiaoJH;
import com.jy.rhin.model.body.zhuanzhenjianyi.ZhuanZhenJY;
import com.jy.rhin.model.body.zhuyaojiankangwenti.ZhuYaoJKWT;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2023/2/22
 * @Description
 */
@Service
@Slf4j
public class MenZhenZhaiYaoApplication {

    @Resource
    private MenZhenZhaiYaoService menZhenZhaiYaoService;
    @Resource
    private PatientService patientService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private ShuYuService shuYuService;
    @Resource
    private CDACreator cdaCreator;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(ReqDto req) {
        return cdaCreator.create(createMenZhenZhaiYao(req), "17menzhenzhaiyao.ftl");
    }

    public ClinicalDocumentData createMenZhenZhaiYao(ReqDto req) {
        MenZhenInfoVo menZhenInfo = menZhenZhaiYaoService.queryMenZhenInfo(req);
        if (Objects.isNull(menZhenInfo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        BingRenXXDto bingRenInfo = patientService.queryPatientById(menZhenInfo.getPatientId());
        if (Objects.isNull(bingRenInfo)) {
            throw new CDAException("患者信息不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(menZhenInfo.getYiLiaoJGDM()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(menZhenInfo.getYiLiaoJGDM()).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        //前置数据齐备，再来处理CDA
        MenZhenZhaiYaoVo menZhenZhaiYaoVo = new MenZhenZhaiYaoVo();
        menZhenZhaiYaoService.menZhenZhaiYao(req, menZhenZhaiYaoVo);
        shuYuZhiYuQuery(bingRenInfo, menZhenInfo, menZhenZhaiYaoVo);

        MenZhenZY docData = new MenZhenZY();
        docData.setId(menZhenInfo.getMenZhenJZLSH());
        setClinicalDocumentData(docData, menZhenInfo, bingRenInfo, hospital);

        ZhuYaoJKWT zhuYaoJKWT = new ZhuYaoJKWT();
        zhuYaoJKWT.setZiXunWT(StringUtils.isEmpty(menZhenInfo.getZhuSu()) ? "未见异常" : menZhenInfo.getZhuSu());
        zhuYaoJKWT.setWeiShengFWYQ("未见异常");
        docData.setZhuYaoJKWT(zhuYaoJKWT);

        ComponentOf componentOf = new ComponentOf();
        componentOf.setEffectiveTime(CDAUtil.toD15(menZhenInfo.getShuJuSCSJ()));
        componentOf.setRepresentedOrganizationName(menZhenInfo.getKeShiMC());
        componentOf.setWholeOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        componentOf.setWholeOrganizationName(hospital.getName());
        docData.setComponentOf(componentOf);

        ArrayList<ZhengZhuang> zhengZhuangs = new ArrayList<>();
        if (!StringUtils.isEmpty(menZhenInfo.getZhengZhuangDM())) {
            String[] zhengZhuangDMs = menZhenInfo.getZhengZhuangDM().split("\\|");
            String[] zhengZhuangMCs = menZhenInfo.getZhengZhuangMC().split("\\|");
            for (int i = 0; i < zhengZhuangDMs.length; i++) {
                if (zhengZhuangDMs[i].length() >= 7) {
                    ZhengZhuang zhengZhuang = new ZhengZhuang();
                    zhengZhuang.setZhengZhuangDM(zhengZhuangDMs[i].substring(0, 7));
                    zhengZhuang.setZhengZhuangMC(zhengZhuangMCs[i]);
                    zhengZhuang.setKaiShiRQ(CDAUtil.toD15(menZhenInfo.getZhengZhuangKSSJ()));
                    if ("19000101000000".equals(zhengZhuang.getKaiShiRQ())) {
                        zhengZhuang.setKaiShiRQ(CDAUtil.toD15(menZhenInfo.getJiuZhenSJ()));
                    }
                    zhengZhuang.setZhengZhuangCXSJ(Converter.toString(menZhenInfo.getZhengZhuangCXSJ(), "30"));
                    zhengZhuangs.add(zhengZhuang);
                }
            }
        }
        docData.setZhengZhuangList(zhengZhuangs);
        //实验室检查检验
        List<ShiYanShiJCJY> shiYanShiJCJYList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getJianChaInfoList())) {
            shiYanShiJCJYList.addAll(menZhenZhaiYaoVo.getJianChaInfoList().stream().map(JianChaInfoVo::menZhenJianChaVo).collect(Collectors.toList()));
            docData.setShiYanShiJCJYList(shiYanShiJCJYList);
        }
        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getJianYanInfoList())) {
            shiYanShiJCJYList.addAll(menZhenZhaiYaoVo.getJianYanInfoList().stream().map(JianYanInfoVo::menZhenJianYanVo).collect(Collectors.toList()));
            docData.setShiYanShiJCJYList(shiYanShiJCJYList);
        }

        //诊断
        List<MenZhenZD> zhenDuanList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getZhenDuanInfoList())) {
            zhenDuanList.addAll(menZhenZhaiYaoVo.getZhenDuanInfoList().stream().map(ZhenDuanInfoVo::menZhenZhenDuanVo).collect(Collectors.toList()));
            docData.setZhenDuanList(zhenDuanList);
        }

        //用药
        List<YongYao> yongYaoList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getZhongYaoInfoList())) {
            for (ZhongYaoInfoVo zy : menZhenZhaiYaoVo.getZhongYaoInfoList()) {
                YongYao yongYao = new YongYao();
                yongYao.addZhongYaoSYLB(new CodeObject("3", "中草药"));
                yongYao.addYongYaoItem(zy.menZhenZhongYaoVo());
            }
        }
        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getXiYaoInfoList())) {
            for (XiYaoInfoVo xy : menZhenZhaiYaoVo.getXiYaoInfoList()) {
                YongYao yongYao = new YongYao();
                yongYao.addZhongYaoSYLB(new CodeObject("1", "未使用"));
                yongYao.addYongYaoItem(xy.menZhenXiYaoVo());
            }
        }
        docData.setYongYao(yongYaoList);

        //手术
        ArrayList<ShouShu> shouShuList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menZhenZhaiYaoVo.getShouShuInfoList())) {
            shouShuList.addAll(menZhenZhaiYaoVo.getShouShuInfoList().stream().map(ShouShuInfoVo::menZhenShouShuVo).collect(Collectors.toList()));
            docData.setShouShuList(shouShuList);
        }

        //其他医学处置
        docData.setQiTaYXCZList(Collections.singletonList("无"));
        //转诊建议

        ZhuanZhenInfoVo zhuanZhenInfoVo = menZhenZhaiYaoVo.getZhuanZhenInfoVo();
        if (!Objects.isNull(zhuanZhenInfoVo)) {
            ZhuanZhenJY zhuanZhenJY = zhuanZhenInfoVo.menZhenZhuanZhen();
            zhuanZhenJY.setZhuanZhenBZ(menZhenInfo.getShiFouZZ() == 1 ? "true" : "false");
            zhuanZhenJY.setZhuanZhenJGDM(CDAUtil.toJGID10(null));
            zhuanZhenJY.setZhuanZhenJGMC("-");
            zhuanZhenJY.setZhuanZhenKSDM("-");
            zhuanZhenJY.setZhuanZhenKSMC("-");
            zhuanZhenJY.setZhuanZhenYY("-");
            zhuanZhenJY.setZhuanZhenRQ("19000101");
            zhuanZhenJY.setJianKangWYPG("无");
            docData.setZhuanZhenJY(zhuanZhenJY);
        }

        if (Objects.isNull(menZhenZhaiYaoVo.getXiYiMenZhenBingLiVo()) && Objects.isNull(menZhenZhaiYaoVo.getZhongYiMenZhenBingLiVo())) {
            menZhenZhaiYaoVo.setXiYiMenZhenBingLiVo(new XiYiMenZhenBingLiVo("无异常", "无", "无"));
        }

        //治疗计划
        ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
        if (!Objects.isNull(menZhenZhaiYaoVo.getXiYiMenZhenBingLiVo())) {
            //健康评估
            XiYiMenZhenBingLiVo xiYiMenZhenBingLiVo = menZhenZhaiYaoVo.getXiYiMenZhenBingLiVo();
            docData.setJianKangPG(Converter.toString(xiYiMenZhenBingLiVo.getGeXiangPG(), "无异常"));
            zhiLiaoJH.setZhiLiaoFA("无");
            zhiLiaoJH.setChuzhiJH(Converter.toString(xiYiMenZhenBingLiVo.getChuZhiJH(), "无"));
            //健康指导
            docData.setJianKangZDList(Collections.singletonList(Converter.toString(xiYiMenZhenBingLiVo.getZhiLiaoYJ(), "无")));
        } else if (!Objects.isNull(menZhenZhaiYaoVo.getZhongYiMenZhenBingLiVo())) {
            //健康评估
            ZhongYiMenZhenBingLiVo zhongYiMenZhenBingLiVo = menZhenZhaiYaoVo.getZhongYiMenZhenBingLiVo();
            docData.setJianKangPG("无异常");
            zhiLiaoJH.setZhiLiaoFA("无");
            zhiLiaoJH.setChuzhiJH("无");
            //健康指导
            docData.setJianKangZDList(Collections.singletonList(Converter.toString(zhongYiMenZhenBingLiVo.getZhiLiaoYJ(), "无")));
        }

        if (!Objects.isNull(menZhenZhaiYaoVo.getZhiLiaoInfoList())) {
            zhiLiaoJH.setZhiLiaoFA(menZhenZhaiYaoVo.getZhiLiaoInfoList().stream().filter(o -> !StringUtils.isEmpty(o.getJinHouZLFA())).map(ZhiLiaoInfoVo::getJinHouZLFA).findFirst().orElse(null));
        }

        docData.setZhiLiaoJH(zhiLiaoJH);

        //费用
        ArrayList<FeiYong> feiYongList = new ArrayList<>();
        List<FeiYongInfoVo> feiYongInfoList = menZhenZhaiYaoVo.getFeiYongInfoList();

        if (CollectionUtils.isEmpty(feiYongInfoList)) {
            FeiYong feiYong = new FeiYong();
            feiYong.setMenZhenFYFLMC("-");
            feiYong.setMenZhenFYFL(new CodeObject("99", "其他"));
            feiYong.setMenZhenFYJE("0.00");
            feiYong.setZhuYuanFYJSFS(new CodeObject("99", "其他"));
            feiYong.setYiLiaoFKFS(new CodeObject("99", "其他"));
            feiYongList.add(feiYong);
        } else {
            Map<String, List<FeiYongInfoVo>> listMap = feiYongInfoList.stream().collect(Collectors.groupingBy(FeiYongInfoVo::getMingXIFYLB));
            for (Map.Entry<String, List<FeiYongInfoVo>> entry : listMap.entrySet()) {
                FeiYong feiYong = new FeiYong();
                List<FeiYongInfoVo> voList = entry.getValue();
                final FeiYongInfoVo[] infoVo = {null};
                entry.getValue().stream().findFirst().ifPresent(value -> infoVo[0] = value);
                assert infoVo[0] != null;
                feiYong.setMenZhenFYFLMC(infoVo[0].getMingXIFYLBMC());
                feiYong.setMenZhenFYFL(new CodeObject(entry.getKey(), infoVo[0].getMingXIFYLBMC()));
                feiYong.setMenZhenFYJE(Converter.toString(BigDecimal.valueOf(voList.stream().mapToDouble(FeiYongInfoVo::getMingXiXMJE).sum()).setScale(2, BigDecimal.ROUND_HALF_UP)));
                if (StringUtil.isNullOrEmpty(infoVo[0].getZhiFuFS())) {
                    feiYong.setZhuYuanFYJSFS(new CodeObject("99", "其他"));
                } else {
                    feiYong.setZhuYuanFYJSFS(new CodeObject(infoVo[0].getZhiFuFS(), infoVo[0].getZhiFuFSMC()));
                }
                feiYong.setYiLiaoFKFS(new CodeObject(infoVo[0].getYiLiaoFKFSDM(), infoVo[0].getYiLiaoFKFSMC()));
                feiYongList.add(feiYong);
            }
        }
        docData.setFeiYongList(feiYongList);

        setDefaultMenZhenZY(docData);
        return docData;
    }

    /**
     * @param menZhenZhaiYaoVo
     */
    private void shuYuZhiYuQuery(BingRenXXDto bingRenXXDto, MenZhenInfoVo menZhenInfo, MenZhenZhaiYaoVo menZhenZhaiYaoVo) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_2261_1.getCode(), new HashSet<String>(Arrays.asList(bingRenXXDto.getXingBieDM())));

        //症状
        if (!StringUtils.isEmpty(menZhenInfo.getZhengZhuangDM())) {
            String[] zhengZhuangDMs = menZhenInfo.getZhengZhuangDM().split("\\|");
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.ICD_10.getCode(), Arrays.asList(zhengZhuangDMs).stream()
                    .filter(o -> o.length() >= 7).map(o -> o.substring(0, 7)).collect(Collectors.toSet()));
        }
        //西药
        List<XiYaoInfoVo> xiYaoInfoList = menZhenZhaiYaoVo.getXiYaoInfoList();
        if (!CollectionUtils.isEmpty(xiYaoInfoList)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YYTJDM.getCode(), xiYaoInfoList.stream().map(XiYaoInfoVo::getYongYaoTJDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YWSYJLDWDM.getCode(), xiYaoInfoList.stream().map(XiYaoInfoVo::getYaoWuSYJLDWDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV08_50_002.getCode(), xiYaoInfoList.stream().map(XiYaoInfoVo::getYaoPinJXDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YWSYPCDM.getCode(), xiYaoInfoList.stream().map(XiYaoInfoVo::getYaoWuSYPCDM).collect(Collectors.toSet()));
        }
        //中药
        List<ZhongYaoInfoVo> zhongYaoInfoList = menZhenZhaiYaoVo.getZhongYaoInfoList();
        if (!CollectionUtils.isEmpty(zhongYaoInfoList)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YYTJDM.getCode(), zhongYaoInfoList.stream().map(ZhongYaoInfoVo::getYongYaoTJDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YWSYJLDWDM.getCode(), zhongYaoInfoList.stream().map(ZhongYaoInfoVo::getYaoWuSYJLDWDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV08_50_002.getCode(), zhongYaoInfoList.stream().map(ZhongYaoInfoVo::getYaoPinJXDM).collect(Collectors.toSet()));
        }
        //费用
        List<FeiYongInfoVo> feiYongInfoList = menZhenZhaiYaoVo.getFeiYongInfoList();
        if (!CollectionUtils.isEmpty(feiYongInfoList)) {
            feiYongInfoList.forEach(o -> {
                o.setZhiFuFS(StringUtils.isEmpty(o.getZhiFuFS()) || Arrays.asList("09", "10", "11", "12").contains(o.getZhiFuFS()) ? "99" : o.getZhiFuFS());
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV07_10_005.getCode(), feiYongInfoList.stream().filter(o -> !StringUtils.isEmpty(o.getYiLiaoFKFSDM())).map(FeiYongInfoVo::getYiLiaoFKFSDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV07_10_001.getCode(), feiYongInfoList.stream().filter(o -> !StringUtils.isEmpty(o.getMingXIFYLB())).map(FeiYongInfoVo::getMingXIFYLB).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_FYJSFS.getCode(), feiYongInfoList.stream().map(FeiYongInfoVo::getZhiFuFS).collect(Collectors.toSet()));
        }
        List<JianYanInfoVo> jianYanInfoVo = menZhenZhaiYaoVo.getJianYanInfoList();
        if (!CollectionUtils.isEmpty(jianYanInfoVo)) {
            jianYanInfoVo.forEach(o -> {
                o.setJieGuoDM("0".equals(o.getJianYanJGYCBZ()) ? "1" : "1".equals(o.getJianYanJGYCBZ()) ? "2" : "3");
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JCJYJG.getCode(), jianYanInfoVo.stream().map(JianYanInfoVo::getJieGuoDM).collect(Collectors.toSet()));
        }
        List<JianChaInfoVo> jianChaInfoVo = menZhenZhaiYaoVo.getJianChaInfoList();
        if (!CollectionUtils.isEmpty(jianChaInfoVo)) {
            jianChaInfoVo.forEach(o -> {
                o.setJieGuoDM("0".equals(o.getYiChangBZ()) ? "1" : "1".equals(o.getYiChangBZ()) ? "2" : "3");
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JCJYJG.getCode(), jianChaInfoVo.stream().map(JianChaInfoVo::getJieGuoDM).collect(Collectors.toSet()));
        }
        List<ZhenDuanInfoVo> zhenDuanInfoVo = menZhenZhaiYaoVo.getZhenDuanInfoList();
        if (!CollectionUtils.isEmpty(zhenDuanInfoVo)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.ICD_10.getCode(), zhenDuanInfoVo.stream().map(o -> o.getZhenDuanBM().substring(0, 7)).collect(Collectors.toSet()));
        }

        log.info("shuYuZhiYuQuery:{}", Serializer.serialize(zhiJiBSFList));
        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));
        bingRenXXDto.setXingBie(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_2261_1.getCode(), Converter.toString(bingRenXXDto.getXingBieDM()), valueByCode));

        //症状
        if (!StringUtils.isEmpty(menZhenInfo.getZhengZhuangDM())) {
            String[] zhengZhuangDMs = menZhenInfo.getZhengZhuangDM().split("\\|");
            List<String> zhengZhuangMCs = new ArrayList<>();
            for (int i = 0; i < zhengZhuangDMs.length; i++) {
                if (zhengZhuangDMs[i].length() >= 7) {
                    zhengZhuangMCs.add(ZhiJiUtils.getValue(HealthDictionaryEnum.ICD_10.getCode(), zhengZhuangDMs[i].substring(0, 7), valueByCode));
                }
            }
            menZhenInfo.setZhengZhuangMC(String.join("|", zhengZhuangMCs));
        }
        //西药
        if (!CollectionUtils.isEmpty(xiYaoInfoList)) {
            xiYaoInfoList.forEach(info -> {
                info.setYaoPinJXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV08_50_002.getCode(), info.getYaoPinJXDM(), valueByCode));
                info.setYaoWuSYJLDWMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YWSYJLDWDM.getCode(), info.getYaoWuSYJLDWDM(), valueByCode));
                info.setYongYaoTJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YYTJDM.getCode(), info.getYongYaoTJDM(), valueByCode));
                info.setYaoWuSYPCMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YWSYPCDM.getCode(), info.getYaoWuSYPCDM(), valueByCode));
            });
        }
        //中药
        if (!CollectionUtils.isEmpty(zhongYaoInfoList)) {
            zhongYaoInfoList.forEach(info -> {
                info.setYaoPinJXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV08_50_002.getCode(), info.getYaoPinJXDM(), valueByCode));
                info.setYaoWuSYJLDWMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YWSYJLDWDM.getCode(), info.getYaoWuSYJLDWDM(), valueByCode));
                info.setYongYaoTJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YYTJDM.getCode(), info.getYongYaoTJDM(), valueByCode));
            });
        }
        //费用
        if (!CollectionUtils.isEmpty(feiYongInfoList)) {
            feiYongInfoList.forEach(info -> {
                if (!StringUtils.isEmpty(info.getMingXIFYLB())) {
                    info.setMingXIFYLBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV07_10_001.getCode(), info.getMingXIFYLB(), valueByCode));
                }
                if (!StringUtils.isEmpty(info.getYiLiaoFKFSDM())) {
                    info.setYiLiaoFKFSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV07_10_005.getCode(), info.getYiLiaoFKFSDM(), valueByCode));
                }
                info.setZhiFuFSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_FYJSFS.getCode(), info.getZhiFuFS(), valueByCode));
            });
        }
        if (!CollectionUtils.isEmpty(jianYanInfoVo)) {
            jianYanInfoVo.forEach(info -> {
                info.setJieGuoMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JCJYJG.getCode(), info.getJieGuoDM(), valueByCode));
            });
        }
        if (!CollectionUtils.isEmpty(jianChaInfoVo)) {
            jianChaInfoVo.forEach(info -> {
                info.setJieGuoMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JCJYJG.getCode(), info.getJieGuoDM(), valueByCode));
            });
        }
        if (!CollectionUtils.isEmpty(zhenDuanInfoVo)) {
            zhenDuanInfoVo.forEach(info -> {
                info.setZhenDuanMC(ZhiJiUtils.getValue(HealthDictionaryEnum.ICD_10.getCode(), info.getZhenDuanBM().substring(0, 7), valueByCode));
            });
        }
    }

    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData, MenZhenInfoVo menZhenInfo, BingRenXXDto bingRenXXDto, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(menZhenInfo.getShuJuSCSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(bingRenXXDto));
        clinicalDocumentData.setAuthor(createAuthor(menZhenInfo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        return clinicalDocumentData;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(MenZhenInfoVo menZhenInfo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(menZhenInfo.getJiuZhenSJ()));
        author.setId(menZhenInfo.getYiShengDM());
        author.setName(menZhenInfo.getYiShengMC());
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(BingRenXXDto bingRenXXDto) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(bingRenXXDto.getEid()));
        patientRole.setTelecom(bingRenXXDto.getShouJiHao());
        patientRole.setAddr(createAddr(bingRenXXDto));
        patientRole.setPatient(createPatient(bingRenXXDto));
        return patientRole;
    }

    Addr createAddr(BingRenXXDto bingRenXXDto) {
        Addr addr = new Addr();
        addr.setState(bingRenXXDto.getJuZhuDSFMC());
        addr.setCity(bingRenXXDto.getJuZhuDSMC());
        addr.setCounty(bingRenXXDto.getJuZhuDQMC());
        addr.setTownship(bingRenXXDto.getJuZhuDJDMC());
        addr.setStreetName(bingRenXXDto.getJuZhuDSQMC());
        addr.setHouseNumber(bingRenXXDto.getJuZhuDMPH());
        addr.setPostalCode(bingRenXXDto.getLianXiRYB());
        return addr;
    }

    Patient createPatient(BingRenXXDto bingRenXXDto) {
        Patient patient = new Patient();
        patient.setId(bingRenXXDto.getZhengJianHM());
        patient.setName(bingRenXXDto.getBingRenXM());
        patient.setAdministrativeGender(new CodeObject(bingRenXXDto.getXingBieDM(), bingRenXXDto.getXingBie()));
        patient.setBirthTime(CDAUtil.toD8(Converter.toDate(bingRenXXDto.getChuShengRQ())));
        patient.setMaritalStatus(new CodeObject(bingRenXXDto.getHunYinZT(), bingRenXXDto.getHunYinZTMC()));
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuDM()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuMC())) {
            patient.setEthnicGroup(new CodeObject(bingRenXXDto.getMinZuDM(), bingRenXXDto.getMinZuMC()));
        }
        return patient;
    }

    //没有数据的章节设置默认值
    private void setDefaultMenZhenZY(MenZhenZY menZhenZY) {
        if (menZhenZY.getZhuYaoJKWT() == null) {
            ZhuYaoJKWT zhuYaoJKWT = new ZhuYaoJKWT();
            zhuYaoJKWT.setWeiShengFWYQ("未见异常");
            zhuYaoJKWT.setZiXunWT("zhuYaoJKWT");
            menZhenZY.setZhuYaoJKWT(zhuYaoJKWT);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getZhengZhuangList())) {
            List<ZhengZhuang> zhengZhuangList = new ArrayList<>();
            ZhengZhuang zz = new ZhengZhuang();
            zz.setKaiShiRQ("20231118135938");
            zz.setZhengZhuangDM("M50.201");
            zz.setZhengZhuangMC("颈椎间盘突出");
            zz.setZhengZhuangCXSJ("10");
            zhengZhuangList.add(zz);
            menZhenZY.setZhengZhuangList(zhengZhuangList);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getZhenDuanList())) {
            List<MenZhenZD> zhenDuanList = new ArrayList<>();
            MenZhenZD zd = new MenZhenZD();
            zd.setQueZhenSJ("20231118");
            zd.setZhenDuanDM("M50.201");
            zd.setZhenDuanMC("颈椎间盘突出");
            zhenDuanList.add(zd);
            menZhenZY.setZhenDuanList(zhenDuanList);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getShiYanShiJCJYList())) {
            List<ShiYanShiJCJY> shiYanShiJCJYList = new ArrayList<>();
            ShiYanShiJCJY jy = new ShiYanShiJCJY();
            jy.setXiangMuDM("00901");
            jy.setXiangMuMC("尿酸");
            jy.setJieGuoDM(new CodeObject("2", "正常"));
            ShiYanShiJCJY jc = new ShiYanShiJCJY();
            jc.setXiangMuDM("00404");
            jc.setXiangMuMC("肺部CT");
            jc.setJieGuoDM(new CodeObject("2", "正常"));
            shiYanShiJCJYList.add(jc);
            shiYanShiJCJYList.add(jy);
            menZhenZY.setShiYanShiJCJYList(shiYanShiJCJYList);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getYongYao())) {
            menZhenZY.setYongYao(new ArrayList<>());
            YongYao yongYao = new YongYao();
            yongYao.addZhongYaoSYLB(new CodeObject("1", "未使用"));
            YongYao.YongYaoItem item = getYongYaoItem();
            yongYao.addYongYaoItem(item);
            menZhenZY.getYongYao().add(yongYao);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getShouShuList())) {
            List<ShouShu> shouShuList = new ArrayList<>();
            ShouShu shouShu = new ShouShu();
            shouShu.setCaoZuoMC("头和颈部血管治疗性超声");
            shouShu.setShouShuCZ(new CodeObject("00.0100", "头和颈部血管治疗性超声"));
            shouShu.setCaoZuoSJ("20231017142000");
            shouShuList.add(shouShu);
            menZhenZY.setShouShuList(shouShuList);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getQiTaYXCZList())) {
            List<String> qiTaYXCZList = new ArrayList<>();
            qiTaYXCZList.add("无");
            menZhenZY.setQiTaYXCZList(qiTaYXCZList);
        }
        if (menZhenZY.getZhuanZhenJY() == null) {
            ZhuanZhenJY zhuanZhenJY = new ZhuanZhenJY();
            zhuanZhenJY.setZhuanZhenBZ("false");
            zhuanZhenJY.setZhuanZhenJGDM(CDAUtil.toJGID10(null));
            zhuanZhenJY.setZhuanZhenJGMC("-");
            zhuanZhenJY.setZhuanZhenKSDM("-");
            zhuanZhenJY.setZhuanZhenKSMC("-");
            zhuanZhenJY.setZhuanZhenYY("-");
            zhuanZhenJY.setZhuanZhenRQ(CDAUtil.defaultD8());
            zhuanZhenJY.setJianKangWYPG("无");
            menZhenZY.setZhuanZhenJY(zhuanZhenJY);
        }
        if (StringUtil.isEmpty(menZhenZY.getJianKangPG())) {
            menZhenZY.setJianKangPG("无");
        }
        if (menZhenZY.getZhiLiaoJH() == null || menZhenZY.getZhiLiaoJH().getZhiLiaoFA() == null) {
            ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
            zhiLiaoJH.setZhiLiaoFA("无");
            zhiLiaoJH.setChuzhiJH("无");
            menZhenZY.setZhiLiaoJH(zhiLiaoJH);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getJianKangZDList())) {
            List<String> jianKangZDList = new ArrayList<>();
            jianKangZDList.add("无");
            menZhenZY.setJianKangZDList(jianKangZDList);
        }
        if (CollectionUtils.isEmpty(menZhenZY.getFeiYongList())) {
            List<FeiYong> feiYongList = new ArrayList<>();
            FeiYong feiYong = new FeiYong();
            feiYong.setMenZhenFYFL(new CodeObject("04", "诊察费"));
            feiYong.setMenZhenFYFLMC("诊察费");
            feiYong.setMenZhenFYJE("1.00");
            feiYong.setZhuYuanFYJSFS(new CodeObject("01", "现金"));
            feiYong.setYiLiaoFKFS(new CodeObject("07", "全自费"));
            feiYongList.add(feiYong);
            menZhenZY.setFeiYongList(feiYongList);
        }
    }

    public static YongYao.YongYaoItem getYongYaoItem() {
        YongYao.YongYaoItem item = new YongYao.YongYaoItem();
        item.setYongYaoTJ(new CodeObject("1", "口服"));
        item.setDanCiYYJL(new ValueUnit("1.00", "mg"));
        item.setYaoWuSYPL(new ValueUnit("1", "次/日"));
        item.setYaoWuJX(new CodeObject("01", "片剂(素片、压制片),浸膏片,非包衣片"));
        item.setYaoPinMC("感冒灵");
        item.setYaoWuLX("西药");
        item.setYongYaoTS(new ValueUnit("7", "天"));
        item.setYaoWuSYZJL(new ValueUnit("20.00", "mg"));
        item.setYongYaoTZRQ("20240222095944");
        return item;
    }
}
